package com.qf.servlet;

import com.qf.pojo.Profile;
import com.qf.service.ProfileService;
import com.qf.service.impl.ProfileServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @author 29125
 * @version V1.0
 * @Project java2203
 * @Package com.qf.servlet
 * @Description:
 * @Date 2022/5/27 16:13
 */
public class UpdateServlet extends BaseServlet{
    //修改
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //1获取前端数据
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String birthday = request.getParameter("birthday");
        String gender = request.getParameter("gender");
        String career = request.getParameter("career");
        String address = request.getParameter("address");
        String mobile = request.getParameter("mobile");
        String picture = request.getParameter("picture");

        try {
            //2调用业务逻辑
            ProfileService profileService=new ProfileServiceImpl();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Profile profile=new Profile(Integer.parseInt(id),name,sdf.parse(birthday),gender,career,address,mobile,picture);
            profileService.modify(profile);


            //3转发重定向
            response.sendRedirect(request.getContextPath()+"/booklistpage");
        } catch (Exception e) {
//            e.printStackTrace();
            request.setAttribute("msg","更新失败："+e.getMessage());
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }
    }

}
